;; -*- mode:scheme -*-

(c-system-include "X11/Xlib.h")
(c-system-include "X11/Xutil.h")

(c-include "Xlib_helper.c")

;; Integer types
(define-c-int-type XAtom)
(define-c-int-type XColormap)
(define-c-int-type XDrawable)
(define-c-int-type XFont)
(define-c-int-type XGContext)
(define-c-int-type XKeySym)
(define-c-int-type XMask)
(define-c-int-type XPixmap)
(define-c-int-type XStatus)
(define-c-int-type XTime)
(define-c-int-type XVisualID)
(define-c-int-type XWindow)
(define-c-int-type XID)

;; Struct types
(define-c-type XDepth predicate: XDepth?)
(define-c-type XDisplay predicate: XDisplay? finalizer: XDisplayFinalizer)
(define-c-type XEvent predicate: XEvent?)
(define-c-type XGC predicate: XGC?)
(define-c-type XImage predicate: XImage?)
(define-c-type XScreen predicate: XScreen?)
(define-c-type XScreenFormat predicate: XScreenFormat?)
(define-c-type XVisual predicate: XVisual?)

;; Functions from the XOpenDisplay man page
(define-c
  (pointer free XDisplay)
  (XOpenDisplay "XOpenDisplay_w")
  ((maybe-null char*)))
(define-c
  void
  (XCloseDisplay "XCloseDisplay_w")
  ((pointer XDisplay)))

;; Functions from the AllPlanes man page
(define-c
  unsigned-long
  (XBlackPixel "XBlackPixel_w")
  ((pointer XDisplay) int))
(define-c
  unsigned-long
  (XWhitePixel "XWhitePixel_w")
  ((pointer XDisplay) int))
(define-c
  int
  (XConnectionNumber "XConnectionNumber_w")
  ((pointer XDisplay)))
(define-c
  XColormap
  (XDefaultColormap "XDefaultColormap_w")
  ((pointer XDisplay) int))
(define-c
  int
  (XDefaultDepth "XDefaultDepth_w")
  ((pointer XDisplay) int))
;; TODO: XListDepths produces a warning. I don't know why.
;; Xlib.c:193: warning: passing argument 2 of ‘sexp_make_integer’ makes integer from pointer without a cast
;; (define-c (pointer int) XListDepths ((pointer XDisplay) int (pointer int)))
(define-c
  XGC
  (XDefaultGC "XDefaultGC_w")
  ((pointer XDisplay) int))
(define-c
  XWindow
  (XDefaultRootWindow "XDefaultRootWindow_w")
  ((pointer XDisplay)))
(define-c
  (pointer XScreen)
  (XDefaultScreenOfDisplay "XDefaultScreenOfDisplay_w")
  ((pointer XDisplay)))
(define-c
  int
  (XDefaultScreen "XDefaultScreen_w")
  ((pointer XDisplay)))
(define-c
  (pointer XVisual)
  (XDefaultVisual "XDefaultVisual_w")
  ((pointer XDisplay) int))
(define-c
  int
  (XDisplayCells "XDisplayCells_w")
  ((pointer XDisplay) int))
(define-c
  int
  (XDisplayPlanes "XDisplayPlanes_w")
  ((pointer XDisplay) int))
(define-c
  char*
  (XDisplayString "XDisplayString_w")
  ((pointer XDisplay)))
(define-c
  long
  (XMaxRequestSize "XMaxRequestSize_w")
  ((pointer XDisplay)))
(define-c
  long
  (XExtendedMaxRequestSize "XExtendedMaxRequestSize_w")
  ((pointer XDisplay)))
(define-c
  unsigned-long
  (XLastKnownRequestProcessed "XLastKnownRequestProcessed_w")
  ((pointer XDisplay)))
(define-c
  unsigned-long
  (XNextRequest "XNextRequest_w")
  ((pointer XDisplay)))
(define-c
  int
  (XProtocolVersion "XProtocolVersion_w")
  ((pointer XDisplay)))
(define-c
  int
  (XProtocolRevision "XProtocolRevision_w")
  ((pointer XDisplay)))
(define-c
  int
  (XQLength "XQLength_w")
  ((pointer XDisplay)))
(define-c
  XWindow
  (XRootWindow "XRootWindow_w")
  ((pointer XDisplay) int))
(define-c
  int
  (XScreenCount "XScreenCount_w")
  ((pointer XDisplay)))
(define-c
  (pointer XScreen)
  (XScreenOfDisplay "XScreenOfDisplay_w")
  ((pointer XDisplay) int))
(define-c
  char*
  (XServerVendor "XServerVendor_w")
  ((pointer XDisplay)))
(define-c
  int
  (XVendorRelease "XVendorRelease_w")
  ((pointer XDisplay)))

;; Functions from the XCreateWindow man page
(define-c
  XWindow
  (XCreateSimpleWindow "XCreateSimpleWindow_w")
  ((pointer XDisplay) XWindow int int unsigned-int unsigned-int unsigned-int
   unsigned-long unsigned-long))

;; Functions from the XSelectInput man page
(define-c
  int
  (XSelectInput "XSelectInput_w")
  ((pointer XDisplay) XWindow long))

;; Functions from the XMapWindow man page
(define-c
  int
  (XMapWindow "XMapWindow_w")
  ((pointer XDisplay) XWindow))
(define-c
  int
  (XMapRaised "XMapRaised_w")
  ((pointer XDisplay) XWindow))
(define-c
  int
  (XMapSubwindows "XMapSubwindows_w")
  ((pointer XDisplay) XWindow))

;; Functions from the XInternAtom man page
(define-c
  XAtom
  (XInternAtom "XInternAtom_w")
  ((pointer XDisplay) char* int))
(define-c
  XStatus
  (XInternAtoms "XInternAtoms_w")
  ((pointer XDisplay) (pointer char*) int boolean (pointer XAtom)))
(define-c
  char*
  (XGetAtomName "XGetAtomName_w")
  ((pointer XDisplay) XAtom))
(define-c
  XStatus
  (XGetAtomNames "XGetAtomNames_w")
  ((pointer XDisplay) (pointer XAtom) int (pointer char*)))

;; Functions from the XSetWMProtocols man page
(define-c
  XStatus
  (XSetWMProtocols "XSetWMProtocols_w")
  ((pointer XDisplay) XWindow (pointer XAtom) int))
;; TODO: pointer of pointer (Atom**) not supported
;;(define-c XStatus XGetWMProtocols ((pointer XDisplay) XWindow (pointer XAtom*) (pointer int)))

;; Functions from the XNextEvent man page
(define-c
  void
  (XNextEvent "XNextEvent_w")
  ((pointer XDisplay) (result pointer XEvent)))
(define-c
  int
  (XPeekEvent "XPeekEvent_w")
  ((pointer XDisplay) (result pointer XEvent)))
(define-c
  int
  (XWindowEvent "XWindowEvent_w")
  ((pointer XDisplay) XWindow long (result pointer XEvent)))
(define-c
  boolean
  (XCheckWindowEvent "XCheckWindowEvent_w")
  ((pointer XDisplay) XWindow long (result pointer XEvent)))
(define-c
  int
  (XMaskEvent "XMaskEvent_w")
  ((pointer XDisplay) long (result pointer XEvent)))
(define-c
  boolean
  (XCheckMaskEvent "XCheckMaskEvent_w")
  ((pointer XDisplay) long (result pointer XEvent)))
(define-c
  boolean
  (XCheckTypedEvent "XCheckTypedEvent_w")
  ((pointer XDisplay) int (result pointer XEvent)))
(define-c
  boolean
  (XCheckTypedWindowEvent "XCheckTypedWindowEvent_w")
  ((pointer XDisplay) XWindow int (result pointer XEvent)))

;; Functions from the XCreateImage man page
(define-c XStatus XInitImage ((pointer XImage)))
(define-c
  (pointer XImage)
  (XCreateImage "XCreateImage_w")
  ((pointer XDisplay) (pointer XVisual) unsigned-int int int char*
   unsigned-int unsigned-int int int))
(define-c unsigned-long XGetPixel ((pointer XImage) int int))
(define-c int XPutPixel ((pointer XImage) int int unsigned-long))
(define-c (pointer XImage) XSubImage
  ((pointer XImage) int int unsigned-int unsigned-int))
(define-c int XAddPixel ((pointer XImage) long))
(define-c int XDestroyImage ((pointer XImage)))

;; Functions from the XPutImage man page
(define-c
  int
  (XPutImage "XPutImage_w")
  ((pointer XDisplay) XDrawable XGC (pointer XImage) int int int int
   unsigned-int unsigned-int))
(define-c
  (pointer XImage)
  (XGetImage "XGetImage_w")
  ((pointer XDisplay) XDrawable int int unsigned-int unsigned-int
   unsigned-long int))
(define-c
  (pointer XImage)
  (XGetSubImage "XGetSubImage_w")
  ((pointer XDisplay) XDrawable int int unsigned-int unsigned-int
   unsigned-long int (pointer XImage) int int))

;; Functions from the XUnmapWindow man page
(define-c
  int
  (XUnmapWindow "XUnmapWindow_w")
  ((pointer XDisplay) XWindow))
(define-c
  int
  (XUnmapSubwindows "XUnmapSubwindows_w")
  ((pointer XDisplay) XWindow))

;; XEvent (XAnyEvent)
(define-c int XEventType ((pointer XEvent)))
(define-c unsigned-long XEventSerial ((pointer XEvent)))
(define-c boolean XEventSendEvent ((pointer XEvent)))
(define-c (pointer XDisplay) XEventDisplay ((pointer XEvent)))
(define-c XWindow XEventWindow ((pointer XEvent)))

;; XButtonEvent
(define-c boolean (XButtonPressedEvent? "XIsButtonPressedEvent") ((pointer XEvent)))
(define-c boolean (XButtonReleasedEvent? "XIsButtonReleasedEvent") ((pointer XEvent)))
(define-c boolean (XButtonEvent? "XIsButtonEvent") ((pointer XEvent)))
(define-c XWindow (XButtonEventRoot "XButtonEventRoot") ((pointer XEvent)))
(define-c XWindow (XButtonEventSubwindow "XButtonEventSubwindow") ((pointer XEvent)))
(define-c XTime (XButtonEventTime "XButtonEventTime") ((pointer XEvent)))
(define-c int (XButtonEventX "XButtonEventX") ((pointer XEvent)))
(define-c int (XButtonEventY "XButtonEventY") ((pointer XEvent)))
(define-c int (XButtonEventXRoot "XButtonEventXRoot") ((pointer XEvent)))
(define-c int (XButtonEventYRoot "XButtonEventYRoot") ((pointer XEvent)))
(define-c unsigned-int (XButtonEventState "XButtonEventState") ((pointer XEvent)))
(define-c unsigned-int (XButtonEventButton "XButtonEventButton") ((pointer XEvent)))
(define-c boolean (XButtonEventSameScreen "XButtonEventSameScreen") ((pointer XEvent)))

;; XExposeEvent
(define-c boolean (XExposeEvent? "XIsExposeEvent") ((pointer XEvent)))
(define-c int (XExposeEventX "XExposeEventX") ((pointer XEvent)))
(define-c int (XExposeEventY "XExposeEventY") ((pointer XEvent)))
(define-c int (XExposeEventWidth "XExposeEventWidth") ((pointer XEvent)))
(define-c int (XExposeEventHeight "XExposeEventHeight") ((pointer XEvent)))
(define-c int (XExposeEventCount "XExposeEventCount") ((pointer XEvent)))

;; XClientMessageEvent
(define-c boolean (XClientMessageEvent? "XIsClientMessageEvent") ((pointer XEvent)))
(define-c XAtom XClientMessageEventType ((pointer XEvent)))
(define-c int XClientMessageEventFormat ((pointer XEvent)))
(define-c long XClientMessageEventDataAtom0 ((pointer XEvent)))

;; Some getter functions for other struct fields
(define-c int XVisualClass ((pointer XVisual)))
